<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php 
	$ci = & get_instance();
	$mid = intval($_GET['mid']);
	$MATCH = $ci->match_entity->init("id", $mid);
	
	if(!$MATCH){
		show_404();exit();
	}
	
	$score = $ci->db->
			where( array('match_id'=>$MATCH->getFeedMatchId())) //'type'=>$MATCH->getFeedType(),'key'=>$MATCH->getFeedKey(),
			->order_by('datetime','DESC')->get(TBL_SCORECARD)->result();
	
	$checkHistory = $this->db->where('id',$MATCH->getId())->get(TBL_MATCH_HISTORY)->result();
	
	if(! $checkHistory){
		$inningArray = $this->feed->getInning($type=$MATCH->getFeedType(),$key=$MATCH->getFeedKey(),$match_id_post=$MATCH->getFeedMatchId());
		$currentInfo = $this->feed->getCurrentMatchInfo($type=$MATCH->getFeedType(),$key=$MATCH->getFeedKey(),$match_id_post=$MATCH->getFeedMatchId());
		if($currentInfo){
			$localteam_name = $currentInfo['localteam_name'];
			$visitorteam_name = $currentInfo['visitorteam_name'];
			$match_status = $currentInfo['match_status'];
			if($match_status == 'Finished'){
				$array_info = array('inningArray' => $inningArray, 'currentInfo' => $currentInfo);
				$this->db->set('id',$mid);
				$this->db->set('match_id',$MATCH->getFeedMatchId());
				$this->db->set('content',serialize($array_info));
				$this->db->insert(TBL_MATCH_HISTORY);
			}
		}
	}else{
		$array_info = unserialize($checkHistory[0]->content);
		$inningArray = $array_info['inningArray'];
		$currentInfo = $array_info['currentInfo'];
		if($currentInfo){
			$localteam_name = $currentInfo['localteam_name'];
			$visitorteam_name = $currentInfo['visitorteam_name'];
		}
	}
	
	$inningNum = array();
	if($inningArray){
		foreach($inningArray as $k =>$val){
			$inningNum[] = $val['attributes']['inningnum'];
		}
	}
	$latest = $inningNum ? $inningNum[0] : 0;
?>

<style>
	table.summary{width:99%;float:left;margin-top:15px;border:1px solid #cfcfcf;}
	table.summary thead th{font-weight:bold;padding-left:10px;background:#eeeeee;border-right:1px solid #cfcfcf;border-bottom:1px solid #cfcfcf;text-transform:uppercase;}
	table.summary tbody td{padding-left:10px;border:1px solid #cfcfcf;}
	table tbody tr:nth-child(2n){background:#fff;}
</style>

<div id="center_content">
	<div class="clrboth"></div>
	<div id="content-wrap-element" class="block">
		<div class="content-wrap" style="position:relative;">
			<a href="javascript:void(0);" style="color:#006699;" onclick="javascript:history.back();" class="back_button" title="Back?">
				Back
			</a>
			<div style="margin:5px;">
				<div class="center-align"><h3>Match: <?php echo $MATCH->getTitle();?></h3></div>
				
				<div class="clrboth"></div>
			 	
				<div id="inning-element">
					<?php if($inningArray):?>
						
						<?php foreach($inningArray as $k => $val):?>	
							<?php if($latest == $val['attributes']['inningnum']):?>
							
								Inning Name: <?php echo $val['attributes']['name'];?>	<br />
								Team: <?php if($val['attributes']['team'] == 'visitorteam'){echo $visitorteam_name;}else{echo $localteam_name;} ?> <br />
								<?php if($val['batsmanstats']): ?>
									<div class="center-align"><b>Batsmanstats:</b></div><br/>
									<table class="summary">
										<thead>
											<th>batsman</th>
											<th>status</th>
											<th>r</th>
											<th>b</th>
											<th>4s</th>
											<th>6s</th>
											<th>SR</th>
										  
										</thead>
										<tbody>
										<?php foreach($val['batsmanstats'] as $item):?>
											<tr>
												<td><?php echo $item['batsman'];?></td>
												<td><?php echo $item['status'];?></td>
												<td><?php echo $item['r'];?></td>
												<td><?php echo $item['b'];?></td>
												<td><?php echo $item['s4'];?></td>
												<td><?php echo $item['s6'];?></td>
												<td><?php echo $item['sr'];?></td>
											 
											</tr>
										<?php endforeach; ?>	
										</tbody>
									</table>	
								<?php endif; ?>	
							 	
								<?php if(!empty($val['totals'])): ?>		
									<div class="center-align"><b>Totals: </b></div><br/>
									<?php foreach($val['totals'] as $k=> $value):?>
										&nbsp;<b><?php echo $k;?>:</b><?php echo $value."&nbsp;&nbsp;";?>
										
									<?php endforeach; ?>
									<br />
								<?php endif; ?>	
								
								<?php if($val['bowlers']): ?>	
									<div class="center-align"><b>Bowlers:</b></div>	<br/>
									<table class="summary">
										<thead>
											<th>bowler</th>
											<th>o</th>
											<th>m</th>
											<th>r</th>
											<th>w</th>
											<th>nb</th>
											<th>wd</th>
											<th>er</th>
										
										</thead>
										<tbody>
										<?php foreach($val['bowlers'] as $item):?>
											<tr>
												<td><?php echo $item['bowler'];?></td>
												<td><?php echo $item['o'];?></td>
												<td><?php echo $item['m'];?></td>
												<td><?php echo $item['r'];?></td>
												<td><?php echo $item['w'];?></td>
												<td><?php echo $item['nb'];?></td>
												<td><?php echo $item['wd'];?></td>
												<td><?php echo $item['er'];?></td>
												
											</tr>
										<?php endforeach; ?>	
										</tbody>
									</table>	
								<?php endif; ?>	
								
								<?php if($val['partnerships']): ?>	
									<div class="center-align"><b>Partnerships:</b></div><br/>
									<table class="summary">
										<thead>
											<th>partnership</th>
											<th>rb1</th>
											<th>rb2</th>
											<th>ex</th>
											<th>total</th>
										 
										</thead>
										<tbody>
										<?php foreach($val['partnerships'] as $item):?>
											<tr>
												<td><?php echo $item['partnership'];?></td>
												<td><?php echo $item['rb1'];?></td>
												<td><?php echo $item['rb2'];?></td>
												<td><?php echo $item['ex'];?></td>
												<td><?php echo $item['total'];?></td>
											 
											</tr>
										<?php endforeach; ?>	
										</tbody>
									</table>	
								<?php endif; ?>	
								
							<?php endif;?>	 
						<?php endforeach;?>
					<?php endif;?>
				</div>
			 
				<div class="clrboth"></div>
			<!--	
				<div id="over">
					<?php //foreach($score as $item):?>
						<div class="item-element">
							<span class="title-text"><?php //echo stringNumberTextFM($item->over_index + 1);?> Over:</span>
							<span class="value"><?php //echo $item->value;?></span>
						</div>
					<?php //endforeach;?>	
				</div>
			-->	
			</div>		
		</div>
	</div>
</div>